import tnx from '@/tnx.js';
import BaseType from './BaseType.js';

export default class TableNameRule {

    static DELIMITER = '_';

    fixedPrefix = '';
    modulePrefixEnabled = false;
    numberSplit = false;

    getDefaultTableName({packageName, simpleName}) {
        let tableName = this.fixedPrefix;
        if (this.modulePrefixEnabled) {
            let modulePrefix = BaseType.getModuleName(packageName || '');
            if (modulePrefix) {
                tableName += modulePrefix + TableNameRule.DELIMITER;
            }
        }
        let array = simpleName.splitByUpperCaseLetter(this.numberSplit);
        tableName += array.join(TableNameRule.DELIMITER).toLowerCase();
        return tableName;
    }

    getBaseTableName(tableName) {
        let baseTableName = tableName;
        if (this.fixedPrefix) {
            baseTableName = tableName.substring(this.fixedPrefix.length);
        }
        return baseTableName;
    }

    static of(object) {
        if (typeof object !== 'object') {
            return undefined;
        }
        let rule = new TableNameRule();
        rule.update(object);
        return rule;
    }

    update(object) {
        return tnx.util.object.rewriteSimpleDefined(this, object);
    }

    clone() {
        return TableNameRule.of(this);
    }

}
